package com.twd.aio;

import jdk.nashorn.internal.codegen.CompilerConstants;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

public class ttttttttt {
    public static void main(String[] args) throws IOException {
        ByteBuffer allocate = ByteBuffer.allocate(1024);
        allocate.flip();
        SocketChannel open = SocketChannel.open();
        allocate.put("aa".getBytes());

        A a = new A();
        Task task = new Task(a);
        a.setL(()->{
            System.out.println(task.get());
            System.out.println("back");
        });

        task.start();
        if(task.getF().isDone()){
            System.out.println("sujfw");
        }
        System.out.println("89789797979");
    }
}
interface listener{
    public void doTask() throws ExecutionException, InterruptedException;
}
class Task{
    FutureTask<String> f;

    public Task(A a){
        f=new FutureTask<String>(()->{
            TimeUnit.SECONDS.sleep(5);
            System.out.println("1111111111111");
           new Thread(()->{
               try {
                   a.dd();
               } catch (ExecutionException e) {
                   e.printStackTrace();
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }).start();
           return "aaaaaa";
        });
    }
    public FutureTask<String> getF() {
        return f;
    }

    public void setF(FutureTask<String> f) {
        this.f = f;
    }
    public void start(){
        new Thread(f).start();
    }
    public String get() throws ExecutionException, InterruptedException {
        return f.get();
    }
}

/**
 * 任务执行完就执行监听器方法，
 *
 */
class A{
    listener l;

    public listener getL() {
        return l;
    }

    public void setL(listener l) {
        this.l = l;
    }
    public void dd() throws ExecutionException, InterruptedException {
        l.doTask();
    }
}
